﻿Namespace EIB
    Public Enum ConnectionPortCategory As Byte
        TP_UART = 0
        BCU1_PEI16 = 1
        BCU2_PEI10_FT12 = 2 '=FT1.2
        USB = 3
        KNXNETIP = 4
    End Enum


    Public NotInheritable Class ConnectionPort
        Private _portName As String
        Private _category As ConnectionPortCategory


        Sub New(portName As String, category As ConnectionPortCategory)
            _portName = portName
            _category = category
        End Sub


        Public ReadOnly Property ID As String
            Get
                Return String.Format("{0}\{1}", CategoryToInternalString(_category), _portName.ToUpper)
            End Get
        End Property

        Public ReadOnly Property PortName As String
            Get
                Return _portName
            End Get
        End Property

        Public ReadOnly Property Category As ConnectionPortCategory
            Get
                Return _category
            End Get
        End Property


        Public Function GetAccessModuleType() As Type
            Return GetAccessModuleType(Me)
        End Function

        Public Shared Function GetAccessModuleType(port As ConnectionPort) As Type
            Select Case port.Category
                Case EIB.ConnectionPortCategory.TP_UART
                    Return Nothing
                Case EIB.ConnectionPortCategory.BCU1_PEI16
                    Return Nothing
                Case EIB.ConnectionPortCategory.BCU2_PEI10_FT12
                    If port.PortName = BusAccess.SimulatorName Then
                        Return GetType(EIB.Bus.EIBBusAccessModule_FT12Simulator)
                    Else
                        Return GetType(EIB.Bus.EIBBusAccessModule_FT12Serial)
                    End If
                Case EIB.ConnectionPortCategory.USB
                    Return Nothing
                Case EIB.ConnectionPortCategory.KNXNETIP
                    If port.PortName = BusAccess.KNXNetIPRouting Then
                        Return GetType(EIB.Bus.EIBBusAccessModule_KNXNetIP_Routing)
                    ElseIf port.PortName = BusAccess.KNXNetIPTunnling Then
                        Return Nothing
                    ElseIf port.PortName = BusAccess.SimulatorName Then
                        Return Nothing
                    End If
            End Select

            Return Nothing
        End Function

        Public Shared Function CategoryToString(category As ConnectionPortCategory) As String
            Select Case category
                Case ConnectionPortCategory.TP_UART
                    Return "TP_UART"
                Case ConnectionPortCategory.BCU1_PEI16
                    Return "SerialPort (BCU1/PEI16)"
                Case ConnectionPortCategory.BCU2_PEI10_FT12
                    Return "SerialPort (BCU2/PEI10/FT1.2)"
                Case ConnectionPortCategory.USB
                    Return "USB"
                Case ConnectionPortCategory.KNXNETIP
                    Return "KNXNet/IP"
                Case Else
                    Return category.ToString
            End Select
        End Function

        Friend Shared Function CategoryToInternalString(category As ConnectionPortCategory) As String
            Select Case category
                Case ConnectionPortCategory.TP_UART
                    Return "TP_UART"
                Case ConnectionPortCategory.BCU1_PEI16
                    Return "BCU1"
                Case ConnectionPortCategory.BCU2_PEI10_FT12
                    Return "BCU2"
                Case ConnectionPortCategory.USB
                    Return "USB"
                Case ConnectionPortCategory.KNXNETIP
                    Return "KNXNetIP"
                Case Else
                    Return category.ToString
            End Select
        End Function
    End Class
End Namespace